﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace AugustVisual
{
    [ToolboxItem(false)]
    public class HeaderColumnRow : ItemsControl, IItemsCollection
    {
        static HeaderColumnRow()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(HeaderColumnRow), new FrameworkPropertyMetadata(typeof(HeaderColumnRow)));
        }
        public event EventHandler ChildHeaderColumnCellChanged;
        DataTemplate dataTemplate;
        //DataTemplate controlTemplate;
        FrameworkElementFactory facGrid;
        FrameworkElementFactory facHeaderColumnCell;
        private FrameworkElement[] frameworkElements = new FrameworkElement[0];
        internal FrameworkElement[] ChildHeaderColumnCells
        {
            get
            {
                return frameworkElements;
            }
            set
            {
                frameworkElements = value;
                ChildHeaderColumnCellChanged?.Invoke(this, EventArgs.Empty);
            }
        }

        protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);
            ColumnsCollection columnsCollection = new ColumnsCollection(this);
            for (int i = 0; i < Items.Count; i++)
            {
                columnsCollection.Add(Items[i]);
            }
            Columns = columnsCollection;
            
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }

        protected override Size MeasureOverride(Size constraint)
        {
            Size size = new Size();
            size.Height = constraint.Height;
            FrameworkElement[] frameworkElements = ChildHeaderColumnCells;
            if (frameworkElements.Length > 0)
            {
                for (int i = 0; i < frameworkElements.Length; i++)
                {
                    size.Width += frameworkElements[i].ActualWidth;

                }
                return size;
            }
            return base.MeasureOverride(constraint);
        }


        public void SelectColumn(HeaderColumnCell headerColumnCell)
        {
            foreach (FrameworkElement var in ChildHeaderColumnCells)
            {
                if (var == headerColumnCell) { }
                else
                {
                    if (((HeaderColumnCell)var).upsortArrow != null && ((HeaderColumnCell)var).downsortArrow != null)
                    {
                        ((HeaderColumnCell)var).upsortArrow.Visibility = Visibility.Hidden;
                        ((HeaderColumnCell)var).downsortArrow.Visibility = Visibility.Hidden;
                        ((HeaderColumnCell)var).SortState = 0;
                    }
                }
            }

        }



        public HeaderColumnRow()
        {
            ItemsPanelTemplate itemsPanelTemplate = new ItemsPanelTemplate();
            FrameworkElementFactory facStackPanel = new FrameworkElementFactory(typeof(StackPanel));
            facStackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
            facStackPanel.SetValue(StackPanel.NameProperty, "StackPanelHorizontal");
            itemsPanelTemplate.VisualTree = facStackPanel;
            ItemsPanel = itemsPanelTemplate;
            Loaded += HeaderColumnRow_Loaded;
        }

        private void HeaderColumnRow_Loaded(object sender, RoutedEventArgs e)
        {
            if (ChildHeaderColumnCells.Length == 0)
            {
                ChildHeaderColumnCells = FrameworkElementHelper.Findchilds(this, typeof(HeaderColumnCell));
            }
            if (ChildHeaderColumnCells.Length > 0)
            {
                foreach (FrameworkElement var in ChildHeaderColumnCells)
                {
                    ((HeaderColumnCell)var).ParentHeaderColumnRow = this;
                }
            }
            InvalidateMeasure();
        }

        private ColumnsCollection colums;
        public ColumnsCollection Columns
        {
            get
            {
                if (colums == null)
                {
                    Columns = new ColumnsCollection(this);
                }
                return colums;
            }
            set
            {
                ColumnsCollection oldvalue = colums;
                colums = value;
                OnColumnsPropertyChanged(oldvalue, colums);
            }
        }

        public virtual void OnColumnsPropertyChanged(object oldvalue, object newvalue)
        {
        }

        public void SetItemsTemplate()
        {
            this.ItemTemplate = null;
            dataTemplate = new DataTemplate();
            facGrid = new FrameworkElementFactory(typeof(Grid));

            facHeaderColumnCell = new FrameworkElementFactory(typeof(HeaderColumnCell));
            facHeaderColumnCell.SetValue(HeaderColumnCell.HorizontalContentAlignmentProperty, HorizontalAlignment.Center);
            facHeaderColumnCell.SetValue(HeaderColumnCell.VerticalContentAlignmentProperty, VerticalAlignment.Center);
            Binding binding1 = new Binding();
            binding1.Path = new PropertyPath("Header");
            facHeaderColumnCell.SetBinding(HeaderColumnCell.ContentProperty, binding1);
            Binding binding2 = new Binding();
            binding2.Path = new PropertyPath("Width");
            binding2.Mode = BindingMode.TwoWay;
            facHeaderColumnCell.SetBinding(HeaderColumnCell.WidthProperty, binding2);
            Binding binding3 = new Binding();
            binding3.Path = new PropertyPath("BackGround");
            facHeaderColumnCell.SetBinding(HeaderColumnCell.BackgroundProperty, binding3);
            Binding binding4 = new Binding();
            binding4.Path = new PropertyPath("DisplayMemberBinding");
            facHeaderColumnCell.SetBinding(HeaderColumnCell.DispBindingProperty, binding4);


            facGrid.AppendChild(facHeaderColumnCell);
            dataTemplate.VisualTree = facGrid;
            this.ItemTemplate = dataTemplate;
        }
    }
}
